package webService;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import message.FileWasChanged;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import Watcher.ChangeType;

import serverDB.Client;
import serverDB.ConnectToDataBase;
import serverManger.Server;
/**
 * upload file to the server
 * @author sl300
 *
 */
public class UploadFileServlet extends HttpServlet{
	/**
	 * Default serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/*
	 * get the post request
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		// get the session
		HttpSession session = req.getSession();
		if (ServletFileUpload.isMultipartContent(req)){
			// parse the message from the website
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
			List fileItemsList=null;
			try {
				fileItemsList = servletFileUpload.parseRequest(req);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			String optionalFileName = "";
			FileItem fileItem = null;
			Iterator it = fileItemsList.iterator();
			String boxName=null;
			while (it.hasNext()){
				FileItem fileItemTemp = (FileItem)it.next();
				if (fileItemTemp.isFormField()){
					if (fileItemTemp.getFieldName().equals("filename"))
						optionalFileName = fileItemTemp.getString();
					else if(fileItemTemp.getFieldName().equals("boxName")){
						boxName=fileItemTemp.getString();
					}
				}
				else
					fileItem = fileItemTemp;
			}
			String fileName=null;
			if (fileItem!=null){
				fileName = fileItem.getName();
				System.out.println("fileName : "+fileName);
			}
			// get the file bytes
			byte[] theFile = fileItem.get();
			Server server = Server.getServerInst();
			// get the checkSum
			Checksum checksum = new CRC32();
			checksum.update(theFile,0,theFile.length-1);
			ConnectToDataBase connectToDataBase = ConnectToDataBase.getInst();
			// get the client
			List<Client> list=connectToDataBase.getClient2("from Client where user_name= '"+session.getAttribute("userName")+"'");
			if(server.fileWasChanged(list.get(0), fileName, checksum.getValue(), theFile.length, boxName, null,new FileWasChanged(fileName, checksum.getValue(), theFile.length, boxName,null,null,ChangeType.CREATE))){
				// create the file
				server.createFile(fileName, theFile, boxName, list.get(0), checksum.getValue(),null,null);
				// update all computers who share this box
				server.updateOtherCompMess(list.get(0), null,new FileWasChanged(fileName, checksum.getValue(), theFile.length, boxName,null,null,ChangeType.CREATE),boxName);
			}
			resp.sendRedirect(resp.encodeRedirectURL("fileWasUploaded.jsp"));		
		}		
	}
}
